package com.liming.server.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liming.server.domain.MemberCourse;
import com.liming.server.domain.MemberCourseExample;
import com.liming.server.dto.MemberCourseDto;
import com.liming.server.dto.PageDto;
import com.liming.server.mapper.MemberCourseMapper;
import com.liming.server.util.CopyUtil;
import com.liming.server.util.UuidUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Date;

@Service
public class MemberCourseService {
    @Resource
    private MemberCourseMapper memberCourseMapper;

    public void list(PageDto pageDto){
        PageHelper.startPage(pageDto.getPage(),pageDto.getSize());
        MemberCourseExample memberCourseExample = new MemberCourseExample();
        List<MemberCourse> memberCourseList = memberCourseMapper.selectByExample(memberCourseExample);
        PageInfo<MemberCourse> pageInfo = new PageInfo<>(memberCourseList);
        pageDto.setTotal(pageInfo.getTotal());
        //将memberCourseList通过CopyUtil.copyList()方法复制给memberCourseDtoList
        List<MemberCourseDto> memberCourseDtoList =CopyUtil.copyList(memberCourseList,MemberCourseDto.class);
        pageDto.setList(memberCourseDtoList);
    }

    public void save(MemberCourseDto memberCourseDto){
        //将memberCourseDto通过CopyUtil.copy()方法复制给MemberCourse
        MemberCourse memberCourse = CopyUtil.copy(memberCourseDto, MemberCourse.class);
        if(StringUtils.isEmpty(memberCourseDto.getId())){
            this.insert(memberCourse);
        }else{
            this.update(memberCourse);
        }
    }

    private void insert(MemberCourse memberCourse){
        Date now = new Date();
        memberCourse.setId(UuidUtil.getShortUuid());
        memberCourse.setAt(now);
        memberCourseMapper.insert(memberCourse);
    }
    private void update(MemberCourse memberCourse){
        memberCourseMapper.updateByPrimaryKey(memberCourse);
    }
    public void delete(String id){
        memberCourseMapper.deleteByPrimaryKey(id);
    }

    /**
     * 报名。先判断是否已报名
     * @param memberCourseDto
     * @return
     */
    public MemberCourseDto enroll(MemberCourseDto memberCourseDto) {
        MemberCourse memberCourseDb = this.select(memberCourseDto.getMemberId(), memberCourseDto.getCourseId());
        if(memberCourseDb == null){
            MemberCourse memberCourse = CopyUtil.copy(memberCourseDto, MemberCourse.class);
            this.insert(memberCourse);
            //将数据库信息全部返回，包括id，at
            return CopyUtil.copy(memberCourse,MemberCourseDto.class);
        }else {
            // 如果已经报名，则返回已报名的信息
            return CopyUtil.copy(memberCourseDb,MemberCourseDto.class);
        }
    }

    /**
     * 根据会员表的memberId和课程表的courseId查询MemberCourse表的信息
     * @param memberId
     * @param courseId
     * @return
     */
    public MemberCourse select(String memberId, String courseId){
        MemberCourseExample example = new MemberCourseExample();
        example.createCriteria()
                .andMemberIdEqualTo(memberId)
                .andCourseIdEqualTo(courseId);
        List<MemberCourse> memberCourseList = memberCourseMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(memberCourseList)){
            return null;
        }else {
            return memberCourseList.get(0);
        }
    }

    /**
     * 获取报名信息
     * @param memberCourseDto
     * @return
     */
    public MemberCourseDto getEnroll(MemberCourseDto memberCourseDto) {
        MemberCourse memberCourse = this.select(memberCourseDto.getMemberId(), memberCourseDto.getCourseId());
        return CopyUtil.copy(memberCourse,MemberCourseDto.class);
    }
}